//
//  LotusAnimation.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 4/8/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "LotusAnimation.h"


@implementation LotusAnimation
@synthesize hasFrog, waveActive;

-(id)initWithFile:(NSString *)filename
{
    if (self = [super initWithFile:filename]) {
        hasFrog = NO;
        
        CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
        NSMutableArray *balls = [[[NSMutableArray alloc] init] autorelease];
        for (int i = 1; i <= 4; ++i) {
            CCSpriteFrame* frame = [cache spriteFrameByName:[NSString stringWithFormat:@"wave_%d.png", i]];
            [balls addObject:frame];
        }
        float rand = arc4random() % 6 + 6;
        wave = [[CCAnimation alloc] initWithSpriteFrames:balls delay:1.0/rand];
    }
    return self;
}

-(void)setWaveActive:(BOOL)waveActive_
{
    waveActive = waveActive_;
    if (self.parent && !waveSprite) {
        waveSprite = [[CCSprite alloc] init];
        [self.parent addChild:waveSprite z:-10];
        waveSprite.position = ccpAdd(self.position, ccp(0, -4));
    }
    if (waveActive == YES) {
        waveSprite.visible = YES;
        [self runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.08 position:ccp(0, -5)], [CCMoveBy actionWithDuration:0.08 position:ccp(0, 5)], nil]];
        [waveSprite runAction:[CCSequence actions:[CCAnimate actionWithAnimation:wave], [CCCallBlock actionWithBlock:^{
            waveSprite.visible = NO;
        }], nil]];
    } else {
        waveSprite.visible = NO;
    }
}
@end
